home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0017_REALFRMT.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  896b  |  33 lines

  1. {
  2.   I recently came across the need For a way to dynamically Format
  3.   Real Variables For output - I came out With the following. (You
  4.   people following the Compiler thread may want this to make your
  5.   Compiler output pretty)
  6.  
  7.   The routine checks to see how big the exponent is; if it's bigger
  8.   than 1E7 or smaller than 1E-7, an unFormatted conversion is made.
  9.   if the number is less than 1E7 and greater than 1E-7, then a
  10.   Formatted String is created. to make the output prettier, trailing
  11.   zeros, periods and leading spaces are deleted.
  12. }
  13.  
  14. Function FormatReal(r:Real):String;
  15. Var
  16.   s :String;
  17.  
  18. begin
  19.   if ((r>1E-7) and (r<1E7))then
  20.     Str(r:12:12, s)
  21.   else
  22.     Str(r, s);
  23.  
  24.   While s[ord(s[0])]='0' do
  25.     Delete(s, ord(s[0]), 1);
  26.   While (s[1]=' ') do
  27.     Delete(s, 1, 1);
  28.   if s[ord(s[0])]='.' then
  29.     Delete(s, ord(s[0]), 1);
  30.  
  31.   FormatReal := s;
  32. end;
  33.